package com.fh.service.loantaskimpl;

import com.fh.mapper.loan.CallLoanInfoMapper;
import com.fh.mapper.loan.SysUserMapper;
import com.fh.mapper.loan.TransferHistoryInfoMapper;
import com.fh.model.Page;
import com.fh.model.loan.SysUser;
import com.fh.model.loan.TransferHistoryInfo;
import com.fh.util.HttpUtils;
import com.fh.util.JsonUtil;
import com.fh.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by xuxiaorong
 */
@Service
public class LoanTaskToBeServiceImpl {

    @Autowired
    private CallLoanInfoMapper callLoanInfoMapper;
    
    @Autowired
    private TransferHistoryInfoMapper transferHistoryInfoMapper;
    
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 获取待催收数据List
     * @param page
     * @return
     */
    public List<PageData> getToBeList(Page page){
        return  callLoanInfoMapper.findTaskToBeListPage(page);
    }
    public List<PageData> getToBeList2(Page page){
        return  callLoanInfoMapper.findTaskToBe2(page);
    }


    /**
     * map 循环去空
     * @param pd 传入参数Map
     */
    public PageData setPdWithTrim(PageData pd){
        //循环去空
        Iterator it = pd.entrySet().iterator();
        Map.Entry entry;
        while (it.hasNext()) {
            entry = (Map.Entry) it.next();
            Object valueObj = entry.getValue();
            pd.put((String) entry.getKey(), null == valueObj ? "" : valueObj.toString().trim());
        }
        return pd;
    }

    /**
     * 获取借款id对应的借款名称
     * @param URL
     * @param mapList
     * @return
     */
    private List<Map<String, String>> getLoanProductsName1(String URL, List<Map<String, String>> mapList){
        List<Map<String, String>> list = new ArrayList();
        String result = HttpUtils.sendPost(URL, new HashMap<String, String>());
        Map map = JsonUtil.jsonToMap(result);
        if (0 == (Integer)map.get("code")) {
            list = (List<Map<String, String>>)  map.get("data");
            for(Map<String, String> lp : list){
                if(lp.containsKey("product_id")){
                    for(Map<String, String> call :mapList){
                        if(call.containsKey("productId")) {
                            if (!lp.get("product_id").equals(call.get("productId"))) continue;
                            call.put("productName", lp.get("product_name"));
                        }
                    }
                }
            }
        }
        return mapList;
    }
    /**
     * 待催收委外
     * @param map
     * @return
     */
    public int updateByIdWeiwai(Map<String,Object> map){
    	int i=callLoanInfoMapper.updateByIdWeiwai(map);
        List<TransferHistoryInfo> transferHistoryInfolist = ( List<TransferHistoryInfo>)map.get("transferHistoryInfoList");
        int j = transferHistoryInfoMapper.batchInsert(transferHistoryInfolist);
		return i;
    }
    
    public SysUser findObjByUserId(String userId){
		return sysUserMapper.findObjByUserId(userId);
    }
    /**
	 * 内催的回到待催收中
	 * @param map
	 * @return
	 */
    public int updateInnerBackOne(Map<String, Object> map){
        List<TransferHistoryInfo> transferHistoryInfolist = ( List<TransferHistoryInfo>)map.get("transferHistoryInfoList");
        transferHistoryInfoMapper.batchInsert(transferHistoryInfolist);
    	return callLoanInfoMapper.updateInnerBackOne(map);
    }
    /**
	 * 外催的回到待催收中
	 * @param map
	 * @return
	 */
    public int updateOutBackOne(Map<String, Object> map){
    	return callLoanInfoMapper.updateOutBackOne(map);
    }
}
